package com.heima.common.exception;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice//controller的增强类 用于统一处理异常
@Slf4j
public class ExceptionCatch {

    //异常处理器 如果抛出了异常会被这个处理类处理
    @ExceptionHandler(Exception.class)
    public ResponseResult exceptionHandler(Exception e){
        log.error("程序出现异常 ==> {}",e.getMessage());
        return  ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);

    }

    /**
     * 自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public ResponseResult CustomException(Exception e){

        //异常对象是否属于自定义异常类型
        if(e instanceof CustomException){
           return  ((CustomException)e).getResponseResult();
        }
        return  ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);

    }
}
